# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: jaewon

import pandas as pd

from experimental.prophet.graph.graph import get_default_graph


def run_from_dataframe(df_in, inputs, outputs, graph=None):
  graph = graph or get_default_graph()
  if not graph.is_instantiated:
    graph.instantiate()

  output_dict = {}
  output_table = []
  for out_var in outputs:
    colname = out_var.name or out_var.instance_name
    output_table.append([])
    output_dict[colname] = output_table[-1]

  nrows, ncols = df_in.shape
  for irow in range(0, nrows):
    # Set inputs.
    for icol in range(0, ncols):
      inputs[icol].value = df_in.iloc[irow, icol]

    # Evaluate graph.
    graph.eval()

    # Set outputs.
    for idx, out_var in enumerate(outputs):
      output_table[idx].append(out_var.value)

  df_out = pd.DataFrame(output_dict, index=df_in.index)
  return df_out
